class BattleController extends Actor dependson (combatants);

var Combatants fighters[10];

var bool allDead;

enum statusType
{
     ST_none,
     ST_poison,
     ST_def,
     ST_atk,
     ST_agi,
     ST_mag,
     ST_acc,
     ST_mute,
     ST_virus,
     ST_stone,
     ST_berserk,
     ST_injury,
     ST_fireAttack,
     ST_waterAttack,
     ST_earthAttack,
     ST_windAttack
};

function checkEndFight()
{
   local Partymember p;
   local EnemyController e;
   local BossPlayerController c;

   //test if the player is dead

   allDead = True;

   foreach AllActors(class 'PartyMember', p)
   {
      if (!e.isInState('dead'))
      {
         allDead = false;
      }
   }
   if (allDead == true)
   {
      //triger game over
   }

   //test if the monsters are dead
   alldead = true;

   foreach AllActors(class 'EnemyController', e)
   {
      if (!e.isInState('dead'))
      {
         allDead = false;
      }
   }
   if (allDead == true)
   {
      foreach AllActors(class 'BossPlayerController', c)
      {
         c.endFight();
      }
   }
}

function bool calculateHit (int dex, int buff, int chance, int target)
{
   if (dex+buff+chance > fighters[target].getTotalAgility())
   {
      return true;
   }
   else
   {
      return false;
   }
}

function doDamage(int target, int damage, int fire, int water, int earth, int wind)
{
   fighters[target].getHurt(damage, fire, water, earth, wind);
}

function changeMP(int target, int damage)
{
   fighters[target].setMP(fighters[target].getMP() + damage);
}

function setStatus(int target, int type, int duration, int effect)
{
   fighters[target].setStatus(duration, effect, type);
}

function startBattle()
{
   /*local int enemiesInBattle;
   local int i;

   enemiesInBattle = rand(6)+1;

   for (i = 4; i < enemiesInBattle+4; i++)
   {
      fighters[i] = Spawn(class'EnemyTofu', , , Location, Rotation);
      fighters[i].HP = 1;
      fighters[i].vitality = 0;
   } */
   gotostate ('active');
}

function loadPlayers()
{
   local int i;
   //local int z;
   local vector v;

   i = 0;
   //place players party in Combatants array slots 0-3
   //for(i=0; i<2; i++)
   //{
      //z = 100*i;
      v.x = -1158.195190;
      v.y = 1461.025513;
      v.z = -88.792038;
      fighters[i] = spawn(class'PartyMemberBattle', none, , v);
      fighters[i].hp = 5;
      fighters[i].maxHP = 5;
      fighters[i].mp = 0;
      fighters[i].maxMP = 0;
      fighters[i].strength = 8;
      fighters[i].dexterity = 10;
      fighters[i].vitality = 10;
      fighters[i].agility = 50;
      fighters[i].intelegence = 5;
      fighters[i].wisdom = 5;
      fighters[i].luck = 5;
      fighters[i].timer = 0;

      v.x = -1300;
      v.y = 1461.025513;
      v.z = -88.792038;
      fighters[i] = spawn(class'EnemyTofu', none, , v);
      fighters[i].hp = 5;
      fighters[i].maxHP = 5;
      fighters[i].mp = 0;
      fighters[i].maxMP = 0;
      fighters[i].strength = 8;
      fighters[i].dexterity = 10;
      fighters[i].vitality = 10;
      fighters[i].agility = 50;
      fighters[i].intelegence = 5;
      fighters[i].wisdom = 5;
      fighters[i].luck = 5;
      fighters[i].timer = 0;
      //`log (i);
   //}
}


state waiting
{
   event Tick (float time)
   {
   }

   function endTurn()
   {
      gotoState ('active');
   }
}

state active
{
   event Tick (float time)
   {
      local PartyMember p;
      local EnemyController e;
      local BossPlayerController c;

      foreach AllActors(class 'PartyMember', p)
      {
         if (p.getTime() >= 100)
         {
            p.timer = 0;
            foreach AllActors(class 'BossPlayerController', c)
            {
               c.startTurn(p);
            }
            gotoState ('waiting');
            return;
         }
      }

      foreach AllActors (class 'EnemyController', e)
      {
         if (e.getTime() >= 100)
         {
            e.timer = 0;
            e.startTurn();
            gotoState ('waiting');
            return;
         }
      }

      foreach AllActors(class 'PartyMember', p)
      {
         p.advanceTime();
      }

      foreach AllActors(class 'EnemyController', e)
      {
         e.advanceTime();
      }
   }
}
